-- ============================================
-- MACHINE A SOUS - ComputerCraft / CC:Tweaged
-- Nécessite : Advanced Computer + Advanced Monitor (couleur)
-- Câblage : relier le Monitor au Computer avec un Wired/Modem
--           ou simplement le coller à côté (peripheral auto)
-- ============================================

-- === CONFIGURATION ===
local SYMBOLS = {
    { name = " \3 ", color = colors.red,      value = 2 },  -- coeur
    { name = " \4 ", color = colors.yellow,   value = 3 },  -- carreau
    { name = " \5 ", color = colors.orange,   value = 5 },  -- trefle (pique visuel)
    { name = " \6 ", color = colors.green,    value = 8 },  -- trefle
    { name = " \15 ", color = colors.lightBlue, value = 15 }, -- soleil/etoile
    { name = " 7 ",  color = colors.magenta,  value = 50 },
}

local MISE = 1          -- points misés à chaque tirage
local points = 100      -- capital de départ (fictif, juste pour le fun)

-- === PERIPHERIQUES ===
local mon = peripheral.find("monitor")
if not mon then
    error("Aucun monitor trouve ! Colle un Advanced Monitor a cote du computer.")
end

mon.setTextScale(1)
local w, h = mon.getSize()

-- === UTILITAIRES D'AFFICHAGE ===
local function centerText(y, text, textColor, bgColor)
    mon.setBackgroundColor(bgColor or colors.black)
    mon.setTextColor(textColor or colors.white)
    local x = math.floor((w - #text) / 2) + 1
    mon.setCursorPos(x, y)
    mon.write(text)
end

local function clear()
    mon.setBackgroundColor(colors.black)
    mon.clear()
end

local function drawFrame()
    clear()
    centerText(1, "=== CASINO COMPUTERCRAFT ===", colors.yellow)
    centerText(2, "MACHINE A SOUS", colors.white)
    centerText(h, "Touche l'ecran pour tirer !", colors.lightGray)
end

-- Dessine les 3 rouleaux avec les symboles donnés (indices dans SYMBOLS)
local function drawReels(i1, i2, i3, highlight)
    local reelY = math.floor(h / 2)
    local slotWidth = math.floor(w / 3)

    local function drawSlot(col, idx)
        local sym = SYMBOLS[idx]
        local x = (col - 1) * slotWidth + math.floor(slotWidth / 2) - 2
        mon.setBackgroundColor(highlight and colors.gray or colors.black)
        mon.setTextColor(sym.color)
        mon.setCursorPos(math.max(1, x), reelY)
        mon.write("[" .. sym.name .. "]")
    end

    mon.setBackgroundColor(colors.black)
    mon.clear()
    centerText(1, "=== CASINO COMPUTERCRAFT ===", colors.yellow)
    centerText(2, "MACHINE A SOUS", colors.white)

    drawSlot(1, i1)
    drawSlot(2, i2)
    drawSlot(3, i3)

    centerText(h - 2, "Points : " .. points, colors.lime)
    centerText(h, "Touche l'ecran pour tirer !", colors.lightGray)
end

-- === LOGIQUE DE TIRAGE ===
local function randomSymbol()
    return math.random(1, #SYMBOLS)
end

-- Petite animation de "roulement" avant le résultat final
local function spinAnimation()
    for step = 1, 12 do
        local i1 = randomSymbol()
        local i2 = randomSymbol()
        local i3 = randomSymbol()
        drawReels(i1, i2, i3, false)
        sleep(0.08 + step * 0.01) -- ralentit progressivement
    end
end

local function evaluateWin(i1, i2, i3)
    if i1 == i2 and i2 == i3 then
        -- 3 identiques : gros gain
        return SYMBOLS[i1].value * MISE, "JACKPOT ! 3x " .. SYMBOLS[i1].name
    elseif i1 == i2 or i2 == i3 or i1 == i3 then
        -- 2 identiques : petit gain
        local matchIdx = (i1 == i2) and i1 or ((i2 == i3) and i2 or i1)
        return math.floor(SYMBOLS[matchIdx].value / 2), "Petit gain ! 2x " .. SYMBOLS[matchIdx].name
    else
        return 0, nil
    end
end

local function playRound()
    if points < MISE then
        drawFrame()
        centerText(math.floor(h/2), "PLUS ASSEZ DE POINTS !", colors.red)
        sleep(2)
        return
    end

    points = points - MISE

    spinAnimation()

    local i1, i2, i3 = randomSymbol(), randomSymbol(), randomSymbol()
    drawReels(i1, i2, i3, true)

    local gain, message = evaluateWin(i1, i2, i3)
    points = points + gain

    if gain > 0 then
        centerText(h - 4, message .. " (+" .. gain .. ")", colors.lime)
    else
        centerText(h - 4, "Perdu... reessaie !", colors.red)
    end

    sleep(1.5)
end

-- === PROGRAMME PRINCIPAL ===
drawFrame()

while true do
    local event, side, x, y = os.pullEvent("monitor_touch")
    playRound()
    drawFrame()
    centerText(h - 2, "Points : " .. points, colors.lime)
end